package tt.dz.mvc.controller.sys;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.charger.OmsChargerRate;
import tt.dz.mvc.controller.base.BaseAction;
import tt.dz.service.charger.ChargerRateService;
import tt.dz.util.ActionUtils;
@Controller
@RequestMapping("/chargerRateApi")
public class ChargerRateAction extends BaseAction
{
	@Autowired
	private ChargerRateService chargerRateServiceImpl;
	
	@RequestMapping("add2Update")
	@ResponseBody
	public JsonCommon<?> add2Update(HttpServletRequest request, HttpServletResponse response)
	{
		String rate_schema = request.getParameter("rate_schema");
		List<OmsChargerRate> list = genarateChargerRateList(request);
		Integer rateSchema = null;
		if (StringUtils.isNumeric(rate_schema))
		{
			rateSchema = Integer.parseInt(rate_schema);
		}
		return chargerRateServiceImpl.add(list, rateSchema);
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JsonCommon<?> delete(HttpServletRequest request, HttpServletResponse response)
	{
		String pRateSchema = request.getParameter("rate_schema");
		String pSerivalNo =  request.getParameter("serival_no");
		return chargerRateServiceImpl.deleteLogic(pRateSchema, pSerivalNo);
	}
	
	@RequestMapping("findPageOrAll")
	@ResponseBody
	public JsonForList<?> findPageOrAll(HttpServletRequest request, HttpServletResponse response)
	{
		OmsCommonParam param = this.buildQueryParam(request, true);
		return chargerRateServiceImpl.findPageObjs(param, ActionUtils.getParamInstance(OmsChargerRate.class, ActionUtils.getParameterMap(request),null,false,true));
	}
	
	private List<OmsChargerRate> genarateChargerRateList(HttpServletRequest request)
	{
//		String rate_schema = request.getParameter("rate_schema");
		String oper_id = this.getLoginUserId(request);
		String chargeMode = request.getParameter("charge_mode");
		String fee_type = request.getParameter("fee_type");
		Integer feeTypeInt = Integer.parseInt(fee_type);
		String[] startTime = request.getParameterValues("startTime");
		String[] endTime = request.getParameterValues("endTime");
		String[] rate = request.getParameterValues("rate");
		List<OmsChargerRate> chargerRates = new ArrayList<OmsChargerRate>();
		if (rate != null && startTime != null && endTime != null)
		{
			for(int i = 0;i < rate.length;i++)
			{
				if (StringUtils.isNumeric(rate[i]) && 
						StringUtils.isNotEmpty(startTime[i]) &&
						StringUtils.isNotEmpty(endTime[i]) )
				{
					OmsChargerRate temp = new OmsChargerRate();
					temp.setFee_type(feeTypeInt);
					temp.setOper_id(oper_id);
					temp.setCharge_mode(Integer.valueOf(chargeMode));
					BigDecimal rateDecimal = new BigDecimal(rate[i]);
					temp.setRate(rateDecimal);
					Time startTimeVal = Time.valueOf(startTime[i]);
					temp.setStart_time(startTimeVal);
					Time endTimeVal = Time.valueOf(startTime[i]);
					temp.setEnd_time(endTimeVal);
					chargerRates.add(temp);
				}
			}
		}
		return chargerRates;
	}
}
